<template>
  <BasicDrawer v-bind="$attrs" :loadingText="vform.loadingText" showFooter title="项目信息" width="70%"
               @ok="handleSubmit($refs.xForm, vform, emit, setDrawerProps, changeLoading, closeDrawer)" @register="registerDrawer">
    <vxe-form title-colon ref="xForm" title-align="right" title-width="100" :data="vform.data" :rules="vform.rules" :loading="vform.loading">
      <vxe-form-item title="项目名称" field="name" :item-render="{}" span="12" title-width="150">
        <template #default="{ data }">
          <vxe-input v-model="data.name" clearable></vxe-input>
        </template>
      </vxe-form-item>
      <vxe-form-item title="所属分类" field="catename" :item-render="{}" span="12" title-width="150">
        <template #default="{ data }">
          <vxe-input v-model="data.catename" suffix-icon="vxe-icon--search" readonly @click="selectCate"></vxe-input>
        </template>
      </vxe-form-item>
      <vxe-form-item title="是否可用" field="avtag" :item-render="{}" span="12" title-width="150">
        <template #default="{ data }">
          <vxe-switch v-model="data.avtag" open-label="是" close-label="否"></vxe-switch>
        </template>
      </vxe-form-item>
      <vxe-form-item title="排序号" field="ornum" :item-render="{}" span="12" title-width="150">
        <template #default="{ data }">
          <vxe-input v-model="data.ornum" type="number"></vxe-input>
        </template>
      </vxe-form-item>
      <vxe-form-item title="项目备注" field="notes" :item-render="{}" span="24" title-width="150">
        <template #default="{ data }">
          <vxe-textarea v-model="data.notes" placeholder="请输入备注" :autosize="{minRows: 4, maxRows: 6}" clearable></vxe-textarea>
        </template>
      </vxe-form-item>
      <vxe-form-item title="创建时间" field="crtim" :item-render="{}" span="12" title-width="150">
        <template #default="{ data }">
          {{ data.crtim }}
        </template>
      </vxe-form-item>
      <vxe-form-item title="更新时间" field="uptim" :item-render="{}" span="12" title-width="150">
        <template #default="{ data }">
          {{ data.uptim }}
        </template>
      </vxe-form-item>

    </vxe-form>
    <TreeModal ref="xTree" @select="cateSelect"/>
  </BasicDrawer>
</template>
<script lang="ts" setup>
import {ref, computed, unref, reactive, toRaw} from 'vue';
import {BasicDrawer, useDrawerInner} from '/@/components/Drawer';
import {useModal} from '/@/components/Modal';
import {defHttp} from '/@/utils/http/axios';
import {VxeFormPropTypes} from 'vxe-table'
import {handleSubmit} from '/@/comps/vxe/edit';
import TreeModal from '/@/comps/org/TreeModal.vue';

const emit = defineEmits(['success', 'register']);

const [registerModal, {openModal}] = useModal();

const vform = reactive({
  loadingText: '',
  url: '/ps/proj/main',
  data: {} as any,
  rules: {
    name: [
      {required: true, message: '请输入名称'},
    ]
  } as VxeFormPropTypes.Rules
})

const xTree = ref({} as any)


const cateName = computed(() => {
  return vform.data.cate ? vform.data.cate.name : '';
});

const [registerDrawer, {setDrawerProps,changeLoading, closeDrawer}] = useDrawerInner(async (data) => {
  vform.loadingText = "加载中";
  changeLoading(true);
  if (data.record.id) {
    vform.data = await defHttp.get({url: vform.url + '/one/' + data.record.id});
    if(vform.data.cate!=null){
      vform.data.catename = vform.data.cate.name;
    }
  } else {
    vform.data = data.record;
    if (vform.data.cate && vform.data.cate.id === '') {
      vform.data.cate = null;
    }
    vform.data.avtag = true;
  }
  changeLoading(false);
});

function selectCate() {
  xTree.value.open({url:"/ps/proj/cate/tree"});
}

function cateSelect(data) {
  vform.data.cate = data;
  if (data != null) {
    vform.data.catename = data.name;
    vform.data.cateid = data.id;
  } else {
    vform.data.catename = null;
    vform.data.cateid = null;
  }
}


</script>

<style>
/*.vxe-input .type--text .is--readonly input{*/
/*  cursor:pointer !important;*/
/*}*/
</style>
